package com.jhx.common.app

import android.app.Application
import android.content.pm.PackageManager
import com.jhx.common.cache.SimpleCache

/**
 *
 * @author Jack 2018-03-05 17:47
 */
class CommonModule {

    companion object {
        private var mApp: Application? = null
        private val KEY_DEBUG = "is_debug"

        fun init(app: Application) {
            mApp = app

        }

        fun getApplication(): Application? {
            return mApp
        }

        fun getVersionCode(): Int {
            var versionCode = 0
            try {
                //获取软件版本号，对应AndroidManifest.xml下android:versionCode
                versionCode = mApp?.packageManager?.getPackageInfo(mApp?.packageName, 0)?.versionCode ?: 0
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return versionCode
        }

        fun getVersion(): String? {
            var versionName: String? = ""
            try {
                //获取软件版本号，对应AndroidManifest.xml下android:versionCode
                versionName = mApp?.packageManager?.getPackageInfo(mApp?.packageName, 0)?.versionName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }

            return versionName
        }

        fun debug(isDebug: Boolean) {
            SimpleCache.put(KEY_DEBUG, isDebug)
        }

        fun isDebug(): Boolean? {
            return SimpleCache.get(KEY_DEBUG)
        }
    }

}